/**
 * 订单工具类
 * 用于处理订单相关功能
 */

/**
 * 生成订单编号
 * @param {string} prefix - 订单前缀，如：RK(入库)、CK(出库)等
 * @returns {string} 生成的订单编号
 */
const generateOrderNumber = (prefix) => {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0');
  const day = String(now.getDate()).padStart(2, '0');
  const hours = String(now.getHours()).padStart(2, '0');
  const minutes = String(now.getMinutes()).padStart(2, '0');
  const seconds = String(now.getSeconds()).padStart(2, '0');
  const random = Math.floor(Math.random() * 1000).toString().padStart(3, '0');
  
  return `${prefix}${year}${month}${day}${hours}${minutes}${seconds}${random}`;
};

/**
 * 计算订单总金额
 * @param {Array} items - 订单项数组
 * @param {string} priceField - 价格字段名称
 * @returns {number} 订单总金额
 */
const calculateOrderAmount = (items, priceField = 'price') => {
  if (!items || !Array.isArray(items) || items.length === 0) {
    return 0;
  }
  
  return items.reduce((total, item) => {
    const price = Number(item[priceField] || 0);
    const quantity = Number(item.quantity || 0);
    return total + (price * quantity);
  }, 0);
};

module.exports = {
  generateOrderNumber,
  calculateOrderAmount
}; 